package org.springblade.modules.admin.vo;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 救援中项目明细
 */
@Data
public class RescueProjectScreenVO {

	@ApiModelProperty(value = "电梯id")
	private Long elevatorId;

	@ApiModelProperty(value = "项目id")
	private Long buildingId;

	@ApiModelProperty(value = "项目名称")
	private String name;

	@ApiModelProperty(value = "经度")
	private BigDecimal longitude;

	@ApiModelProperty(value = "纬度")
	private BigDecimal latitude;

	@ApiModelProperty(value = "电梯地址")
	private String elevatorAddress;

	@ApiModelProperty(value = "任务id")
	private Long planId;

	@ApiModelProperty(value = "工单创建时间")
	private Date createTime;

	@ApiModelProperty(value = "状态 1-调度中，2-待开始，3-已出发，4-已到达")
	private Integer status;

	@ApiModelProperty(value = "工单耗时")
	private Integer workTimeInterval;

	private Integer tempTimeInterval;

	@ApiModelProperty(value = "救援人员明细")
	private List<RescueUserInfoVO> rescueUserInfos;

	public List<RescueUserInfoVO> getRescueUserInfos() {
		if (Func.isNotEmpty(rescueUserInfos)) {
			// 若有人员已到，工单耗时 = 第一个到达人员耗时
			Optional<RescueUserInfoVO> firstUser = rescueUserInfos.stream().filter(t -> t.getSignTime() != null).min(Comparator.comparing(t -> t.getTimeInterval()));
			if (firstUser.isPresent()) {
				this.workTimeInterval = firstUser.get().getTimeInterval();
			}

			// 人员未到达，人员抵达时间 = 工单耗时
			for (RescueUserInfoVO rescueUserInfo : rescueUserInfos) {
				if (Func.isEmpty(rescueUserInfo.getTimeInterval())) {
					rescueUserInfo.setTimeInterval(tempTimeInterval);
				}
			}

		}
		return rescueUserInfos;
	}
}
